﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;
using System.IO;

namespace NEWS.Admin
{
    public partial class News : System.Web.UI.Page
    {
        private static NewsBO newsbo;
        protected void Page_Load(object sender, EventArgs e)
        {
            //if ((Session["LOGIN"] == null) || (int)Session["ROLES"] != 1)
            //{
            //    Response.Redirect("Login.aspx");
            //}
            if (!IsPostBack)
            {
                dldanhmuc.DataSource = CategoryInfo.GetCategoryInfo_BySingle(false, false, false, true, false);
                dldanhmuc.DataBind();
                dldanhmuc.Items.Insert(0, "Tất cả");
                GridView1.DataSource = NewsBOList.GetAllNewBOList();
                GridView1.DataBind();
            }
            string mode = Request.QueryString["Save"];
            //Mode Add
            if (mode == "Success")
            {
                //messagedelete.Text = "Thêm thành công";
                pn_result.Visible = true;
                lblthongbao.Text = "Thêm thành công";
            }
            else if (mode == "this")
            {
                pn_result.Visible = true;
                lblthongbao.Text = "Cập nhật thành công";
            }

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            int newsid_query = Convert.ToInt32(GridView1.SelectedRow.Cells[3].Text);
            Response.Redirect("Edit_AddNews.aspx?Mode=" + newsid_query);

        }

        protected void btndangtin_Click(object sender, EventArgs e)
        {
            Response.Redirect("Edit_AddNews.aspx");
        }

        protected void btnxoatin_Click(object sender, EventArgs e)
        {
            bool test = false;
            foreach (GridViewRow gvRow in GridView1.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                if (chkSel.Checked == true)
                {
                    try
                    {
                        test = true;
                        int newsid = Convert.ToInt32(gvRow.Cells[3].Text);
                        newsbo = NewsBO.GetNewsBO(newsid);
                        deleteExitsImage(newsbo.UrlImage);
                        NewsBO.DeleteNewsBO(newsid);
                    }
                    catch
                    {
                        pn_result.Visible = true;
                        lblthongbao.Text = "Xoá thất bại";
                        return;
                    }
                }
            }
            if(test==false)
            {
                ScriptManager.RegisterStartupScript(Page,Page.GetType(),"Success","alert('Chưa chọn thông tin cần xóa!!!')",true);
                return;
            }
            else
            {
                dldanhmuc_SelectedIndexChanged(sender, e);
                pn_result.Visible = true;
                lblthongbao.Text = "Xoá thành công";
            }
        }

        private void deleteExitsImage(string filedelete)
        {
            try
            {
                var pathFileImage = Server.MapPath("~/TotalUpload/ImagesTotal/" + filedelete);
                FileInfo fileInfo;
                if (File.Exists(pathFileImage))
                {
                    fileInfo = new FileInfo(pathFileImage);
                    fileInfo.Delete();
                }
            }
            catch
            {
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string strScript = "SelectDeSelectHeader(" + ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).ClientID + ");";
                    ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).Attributes.Add("onclick", strScript);
                }
            }
            catch
            {
                //report error
            }        
        }

        protected void dldanhmuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            pn_result.Visible = false;
            lblthongbao.Text = "";
            if (dldanhmuc.SelectedValue == "Tất cả")
            {
                GridView1.DataSource = NewsBOList.GetAllNewBOList();
                GridView1.DataBind();
            }
            else
            {
                GridView1.DataSource = NewsBOList.GetNewsBOList_ByID(Convert.ToInt32(dldanhmuc.SelectedValue));
                GridView1.DataBind();
            }
            messagedelete.Text = "";
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.BindGrid();
        }

        public void BindGrid()
        {
            if (dldanhmuc.SelectedValue == "Tất cả")
            {
                GridView1.DataSource = NewsBOList.GetAllNewBOList();
                GridView1.DataBind();
            }
            else
            {
                GridView1.DataSource = NewsBOList.GetNewsBOList_ByID(Convert.ToInt32(dldanhmuc.SelectedValue));
                GridView1.DataBind();
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Xem")
            {
                GridViewRow selectrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                string Title = ((LinkButton)(((Control)e.CommandSource).Parent.Parent).FindControl("lbtxemtin")).Text;
                Response.Redirect("../ChiTietTinTuc.aspx?idnews=" + selectrow.Cells[3].Text + "&idcate=" + NewsBO.GetNewsBO(int.Parse(selectrow.Cells[3].Text)).CategoryID);
            }
        }
        //protected void btnhuybo_Click(object sender, EventArgs e)
        //{
        //    PanelActions.Visible = false;
        //}
    }
}
